package com.TP.www.servlet;

import com.TP.www.po.Good;
import com.TP.www.service.GoodPort;
import com.TP.www.service.impl.GoodImpl;

import java.util.List;

import static com.TP.www.constant.Constant.SELECT_GOODS_BY_STORE_ID;
import static com.TP.www.constant.Constant.SYS_SC;

/**
 * @author 邱彦祥
 * 商品界面
 */
public class GoodsMenu {
    /**
     * 展示商品界面
     * @param storeId 店铺id
     */
    public static void show(int storeId){
        GoodImpl goodImpl = new GoodImpl();
        while(true){
            List<Good> goods = GoodPort.getGoodByStoreId(SELECT_GOODS_BY_STORE_ID,storeId);
            int i = 1;
            int num;
            String choose;
            System.out.println("****************商品界面*************");
            if (goods.isEmpty() || judge(goods)) {
                System.out.println("暂无商品....");
            }else {
                for (Good good : goods) {
                    if (good.getState() == 1) {
                        System.out.println(i + "商品名: " + good.getGoodName() + " 单价: " + good.getPrice());
                        i++;
                    }
                }
            }
            System.out.println("**************************************");
            System.out.println("*\t\t0.返回上级\t\t\t\t*");
            System.out.println("*\t\t1.上架商品\t\t\t\t*");
            System.out.println("*\t\t2.修改商品信息\t\t\t*");
            System.out.println("*\t\t3.下架商品\t\t\t\t*");
            System.out.println("*********************************");
            System.out.println("请输入数字指令：");
            choose = SYS_SC.nextLine();
            switch (choose){
                case "0":
                    return;
                case "1":
                    goodImpl.addGood(storeId);
                    break;
                case "2":
                    num = getChoose(i);
                    goodImpl.modifyGood(goods.get(num-1),storeId);
                    break;
                case "3":
                    num = getChoose(i);
                    goodImpl.offGood(goods.get(num-1),storeId);
                    break;
                default:
                    System.out.println("输入有误!请重新输入....");
            }
        }
    }

    /**
     * 获取选择
     * @param maxNum 选择最大值
     * @return 选择
     */
    public static int getChoose(int maxNum){
        while (true){
            try{
                String num;
                System.out.println("请输入要操作商品的序号:");
                num = SYS_SC.nextLine();
                while(Integer.parseInt(num) <= 0 || Integer.parseInt(num) >= maxNum){
                    System.out.println("输入有误！请重新输入....");
                    num = SYS_SC.nextLine();
                }
                return Integer.parseInt(num);
            }catch (NumberFormatException e){
                System.out.println("输入有误！请重新输入....");
            }
        }
    }

    /**
     * 判断商品是否为上架状态
     * @param goods 商品
     * @return 商品是否上架
     */
    public static boolean judge(List<Good> goods){
        for (Good good : goods) {
            if (good.getState() == 1) {
                return false;
            }
        }
        return true;
    }
}
